package counter.com.baomidou.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import counter.com.baomidou.entity.Trojan;
import counter.com.baomidou.entity.Virtualmachineinfo;
import counter.com.baomidou.service.IVirtualmachineinfoService;
import counter.com.common.Result;
import counter.com.connproperty.virtualmachineinfo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.Arrays;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-10-07
 */
@RestController
@RequestMapping("/virtualmachineinfo")
public class VirtualmachineinfoController {
    private static final Integer STATUS_FAIL = 0;
    private static final Integer STATUS_GOING = 1;
    private static final Integer STATUS_SUCCESS = 2;
    @Autowired
    IVirtualmachineinfoService service;
    private String DEFAULTSYSTEM = "Centos 6.0";

    @PostMapping("/search")
    public Result search(@RequestBody VirtualMachineSearchProperty data) {
        LambdaQueryWrapper<Virtualmachineinfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Virtualmachineinfo::getId);
        Page<Virtualmachineinfo> page = new Page<>(data.getPage(), data.getPagesize());
        IPage<Virtualmachineinfo> iPage = service.page(page, wrapper);
        return Result.success(iPage);
    }

    @PostMapping("/add")
    public Result add(@RequestBody VirtualMachineAddProperty data) {
        Virtualmachineinfo virtualmachine = new Virtualmachineinfo();
        virtualmachine.setName(data.getName());
        virtualmachine.setSize((long) 0);
        virtualmachine.setDescription(data.getDescription());
        virtualmachine.setSystem(DEFAULTSYSTEM);
        virtualmachine.setCreatedtime(LocalDateTime.now());
        virtualmachine.setStatus(STATUS_GOING);
        if (service.save(virtualmachine)) {
            return Result.success(virtualmachine.getId());
        } else {
            return Result.fail("添加失败");
        }
    }

    @PostMapping("/add_2")
    public Result add_2(@RequestBody VirtualMachineAdd2Property data) {
        Virtualmachineinfo virtualmachine = service.getById(data.getId());
        if (virtualmachine == null) {
            return Result.fail("未找到对象");
        }
        virtualmachine.setFilename(data.getFilename());
        virtualmachine.setFilepath(data.getFilepath());
        virtualmachine.setSize(data.getSize());
        virtualmachine.setStatus(STATUS_SUCCESS);
        if (service.updateById(virtualmachine)) {
            return Result.success("添加成功");
        } else {
            return Result.fail("添加失败");
        }
    }

    @PostMapping("/add_fail")
    public Result add_fail(@RequestBody VirtualMachineAddFailProperty data) {
        Virtualmachineinfo virtualmachine = service.getById(data.getId());
        if (virtualmachine == null) {
            return Result.fail("未找到对象");
        }
        virtualmachine.setStatus(STATUS_FAIL);
        if (service.updateById(virtualmachine)) {
            return Result.success("上传失败，更新完成");
        } else {
            return Result.fail("未知的失败");
        }
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody VirtualMachineDeleteProperty data) {
        if (service.removeByIds(Arrays.asList(data.getIds()))) {
            return Result.success("删除成功");
        } else {
            return Result.fail("删除失败");
        }
    }
}
